package com.zmn.plat.dubbo.impl.orderconfig.returnvisitcontent;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.dto.orderconfig.returnvisitcontent.ReturnVisitQuestionDIO;
import com.zmn.plat.common.dto.orderconfig.returnvisitcontent.ReturnVisitQuestionDRO;
import com.zmn.plat.dubbo.interfaces.orderconfig.returnvisitcontent.ReturnVisitQuestionListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.orderconfig.returnvisitcontent.ReturnVisitQuestion;
import com.zmn.plat.model.entity.orderconfig.returnvisitcontent.ReturnVisitQuestionQuery;
import com.zmn.plat.services.interfaces.orderconfig.returnvisitcontent.ReturnVisitQuestionService;

import lombok.extern.slf4j.Slf4j;

/**
 * 类描述:回访问题dubbo实现类
 *
 * @auther: tanghongjun
 * @since: 2020年4月15日11:27:03
 */
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
@Slf4j
public class ReturnVisitQuestionListRemoteServiceImpl implements ReturnVisitQuestionListRemoteService {

    @Resource
    ReturnVisitQuestionService returnVisitQuestionService;

    @Override
    @ReadOnlyConnection
    public ResponseDTO<ReturnVisitQuestionDRO> getById(Integer questionId) {
        log.info("getById 参数为：questionId={}", questionId);
        if (Objects.isNull(questionId)) {
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        ReturnVisitQuestion returnVisitQuestion = returnVisitQuestionService.findById(questionId);

        if (Objects.isNull(returnVisitQuestion)) {
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }

        ReturnVisitQuestionDRO returnVisitQuestionDRO = new ReturnVisitQuestionDRO();
        BeanUtils.copyProperties(returnVisitQuestion, returnVisitQuestionDRO);
        return ResponseDTO.success(returnVisitQuestionDRO);
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<ReturnVisitQuestionDRO>> listByQuery(ReturnVisitQuestionDIO returnVisitQuestionDIO) {
        log.info("listByQuery 参数为：returnVisitQuestionDIO={}", returnVisitQuestionDIO);
        if (Objects.isNull(returnVisitQuestionDIO)) {
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        ReturnVisitQuestionQuery questionoryQuery = new ReturnVisitQuestionQuery();
        BeanUtils.copyProperties(returnVisitQuestionDIO, questionoryQuery);
        List<ReturnVisitQuestion> returnVisitQuestionoryList = returnVisitQuestionService.listByQuery(questionoryQuery);

        if (CollectionUtil.isNullOrEmpty(returnVisitQuestionoryList)) {
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }

        List<ReturnVisitQuestionDRO> dros = returnVisitQuestionoryList.stream().map(returnVisitQuestionory -> {
            ReturnVisitQuestionDRO returnVisitQuestionoryDRO = new ReturnVisitQuestionDRO();
            BeanUtils.copyProperties(returnVisitQuestionory, returnVisitQuestionoryDRO);
            return returnVisitQuestionoryDRO;
        }).collect(Collectors.toList());

        return ResponseDTO.success(dros);
    }
}
